#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int f[N],a[N],s[N];
int st[17];
int p[N*5],la[N];
int n,nn,add=0,ans=0;
int check(){
    int res=0;
    for(int i=1;i<=n;i++){
        int j=la[st[i]];
        res+=(a[i]==a[j])*a[i];
        la[st[i]]=i;
    }
    la[0]=la[1]=0;
    return res;
}
void dfs(int d){
    if(d==n+1){
        ans=max(ans,check());
        return;
    }
    st[d]=0;
    dfs(d+1);
    st[d]=1;
    dfs(d+1);
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T;
    scanf("%lld",&T);
    while(T--){
        n=0,add=0,ans=0;
        memset(p,0,sizeof p);
        memset(s,0,sizeof s);
        memset(f,0,sizeof f);
        memset(st,0,sizeof st);
        scanf("%lld",&nn);
        if(nn<=15){
            n=nn;
            for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
            dfs(1);
            printf("%lld\n",ans);
            continue;
        }
        for(int i=1;i<=nn;i++){
            int x;
            scanf("%lld",&x);
            if(x!=a[n])a[++n]=x;
            else add+=x;
        }
        for(int i=2;i<=n;i++){
            s[i]=s[i-1];
            if(a[i]==a[i-1])s[i]+=a[i];
        }
        for(int i=1;i<=n;i++){
            la[i]=p[a[i]];
            p[a[i]]=i;
        }
        for(int i=1;i<=n;i++){
            f[i]=max(f[i-1],f[la[i]]+a[i]*(la[i]!=0)+s[i-1]-s[la[i]]+(la[la[i]+1]!=0)*a[la[i]+1]);
        }
        printf("%lld\n",f[n]+add);
    }
    return 0;
}
